/*
 *  Copyright (c) 2010 Yawning <yawninglol at gmail.com>
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   * Neither the name of the Mew Developers nor the
 *     names of its contributors may be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 *  THE POSSIBILITY OF SUCH DAMAGE.
 */

package yawning.mew;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Properties;

import yawning.mew.ProcEffect.Effect;

/**
 * Proc effects.
 *
 * @author Yawning <yawninglol at gmail.com>
 */
public class Procs {
    protected Procs() {
        spellProcs = new HashMap<Integer, ProcEffect>();
        itemProcs = new HashMap<Integer, ProcEffect>();
        potionProcs = new HashMap<Integer, ProcEffect>();
    }

    // Singleton instance.
    private static Procs instance = null;

    public static synchronized Procs getInstance() {
        if (instance == null) {
            instance = new Procs();
        }

        return instance;
    }

    private Map<Integer, ProcEffect> spellProcs;
    private Map<Integer, ProcEffect> itemProcs;
    private Map<Integer, ProcEffect> potionProcs;

    public void registerProcByItemID(int id, ProcEffect p) {
        itemProcs.put(id, p);
    }

    public void registerProcBySpellID(int id, ProcEffect p) {
        spellProcs.put(id, p);
    }

    public void registerProcByPotionID(int id, ProcEffect p) {
        potionProcs.put(id, p);
    }

    public ProcEffect getProcByItemID(int id) {
        ProcEffect p = itemProcs.get(new Integer(id));

        if (p == null) {
            p = new ProcEffect();
            p.name = "Item ID - " + id;
            p.set(Effect.SPELL_ID, id);
            p.triggers = EnumSet.of(ProcEffect.Trigger.USER);
        }

        return p;
    }

    public ProcEffect getProcBySpellID(int id) {
        ProcEffect p = spellProcs.get(new Integer(id));

        if (p == null) {
            p = new ProcEffect();
            p.name = "Spell ID - " + id;
            p.set(Effect.SPELL_ID, id);
            p.triggers = EnumSet.of(ProcEffect.Trigger.USER);
        }

        return p;
    }

    public ProcEffect getProcByPotionID(int id) {
        ProcEffect p = potionProcs.get(new Integer(id));

        if (p == null) {
            p = new ProcEffect();
            p.name = "Potion ID - " + id;
            p.set(Effect.SPELL_ID, id);
            p.triggers = EnumSet.of(ProcEffect.Trigger.POTION);
        }

        return p;
    }

    public List<ProcEffect> getProcsByProperty(Properties p) {
        List<ProcEffect> rval = new ArrayList<ProcEffect>();
        ProcEffect eff;

        // Items.
        String itemProps = p.getProperty("Procs.items");
        if (itemProps != null) {
            String[] res = itemProps.split(",");
            for (int i = 0; i < res.length; i++) {
                eff = getProcByItemID(Integer.parseInt(res[i]));

                if (eff != null) {
                    rval.add(eff);
                }
            }
        }

        // Spells.
        String spellProps = p.getProperty("Procs.spells");
        if (spellProps != null) {
            String[] res = spellProps.split(",");
            for (int i = 0; i < res.length; i++) {
                eff = getProcBySpellID(Integer.parseInt(res[i]));

                if (eff != null) {
                    rval.add(eff);
                }
            }
        }

        // Potion.
        String potionProps = p.getProperty("Procs.potion");
        if (potionProps != null) {
            eff = getProcByPotionID(Integer.parseInt(potionProps));

            if (eff != null) {
                rval.add(eff);
            }
        }

        return rval;
    }
}
